package com.mvp.utils;

import java.math.BigDecimal;

/**
 * Created by zhangyuncai on 2019/4/11.
 * 解决精度问题
 */
public class DecimalUtil {

    private static String removeChar(String inputStr) {
        return inputStr.replaceAll(",", "");
    }

    /**
     * 金钱加法
     */
    public static String add(String v1, String v2) {
        String add = null;
        try {
            v1 = removeChar(v1);
            v2 = removeChar(v2);
            BigDecimal b1 = new BigDecimal(v1);
            BigDecimal b2 = new BigDecimal(v2);
            add = b1.add(b2).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (add == null) {
            add = "";
        }
        return add;
    }

    /**
     * 金钱减法
     */
    public static String subtract(String v1, String v2) {
        String subtract = null;
        try {
          v1 =  removeChar(v1);
          v2 =  removeChar(v2);
            BigDecimal b1 = new BigDecimal(v1);
            BigDecimal b2 = new BigDecimal(v2);
            subtract = b1.subtract(b2).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (subtract == null) {
            subtract = "";
        }
        return subtract;
    }

    /**
     * 金钱乘法
     */
    public static String multiply(String v1, String v2) {
        String multiply = null;
        try {
           v1 = removeChar(v1);
           v2 = removeChar(v2);
            BigDecimal b1 = new BigDecimal(v1);
            BigDecimal b2 = new BigDecimal(v2);
            multiply = b1.multiply(b2).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (multiply == null) {
            multiply = "";
        }
        return multiply;
    }

    /**
     * 乘法
     *
     * @param v1    乘数
     * @param v2    被乘数
     * @param scale 小数点保留位数
     */
    public static String multiplyWithScale(String v1, String v2, int scale) {
        String multiplyWithScale = null;
        try {
          v1 =  removeChar(v1);
          v2 =  removeChar(v2);
            BigDecimal b1 = new BigDecimal(v1);
            BigDecimal b2 = new BigDecimal(v2);
            BigDecimal result = b1.multiply(b2);
            result = result.setScale(scale);
            multiplyWithScale = result.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (multiplyWithScale == null) {
            multiplyWithScale = "";
        }
        return multiplyWithScale;
    }

    /**
     * 金钱除法
     */
    public static String divide(String v1, String v2) {
        String divide = null;
        try {
          v1 =  removeChar(v1);
          v2 =  removeChar(v2);
            BigDecimal b1 = new BigDecimal(v1);
            BigDecimal b2 = new BigDecimal(v2);
            divide = b1.divide(b2).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (divide == null) {
            divide = "";
        }
        return divide;
    }

}
